package top.szhome.system.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.szhome.framework.core.tools.support.Resp;
import top.szhome.framework.starter.db.support.SimpleController;
import top.szhome.system.convert.UserConverter;
import top.szhome.system.dto.UserDTO;
import top.szhome.system.entity.Role;
import top.szhome.system.entity.User;
import top.szhome.system.enums.UserStatus;
import top.szhome.system.qry.UserQry;
import top.szhome.system.service.UserService;
import top.szhome.system.vo.UserVO;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 用户 接口实现
 *
 * @author 唐加利
 * @date 2023/07/31
 **/
@AllArgsConstructor
@RestController
@RequestMapping("/system/user")
@Tag(name = "用户")
public class UserController extends SimpleController<UserService, UserConverter, UserQry, UserDTO, User, UserVO> {

    /**
     * 修改密码
     *
     * @param userId
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @ApiOperationSupport(order = 100)
    @Operation(summary = "修改密码", description = "修改用户密码")
    @Parameter(name = "userId", description = "用户ID", example = "9527")
    @Parameter(name = "oldPassword", description = "原密码", example = "password")
    @Parameter(name = "newPassword", description = "新密码", example = "1qaz@WSX")
    @PostMapping("modify-password")
    public Resp modifyPwd(Long userId, String oldPassword, String newPassword){
        getBaseService().modifyPwd(userId, oldPassword, newPassword);
        return Resp.success();
    }

    /**
     * 批量重置用户密码
     * @param userIds
     * @return
     */
    @ApiOperationSupport(order = 100)
    @Operation(summary = "重置密码", description = "批量重置用户密码")
    @Parameter(name = "userIds", description = "用户ID列表", example = "9527,3561")
    @PostMapping("reset-password")
    public Resp resetPwd(@RequestBody List<Long> userIds) {
        getBaseService().resetPwd(userIds);
        return Resp.success();
    }

    /**
     * 获取用户已分配角色列表
     * @param userId
     * @return
     */
    @Operation(summary = "用户已分配角色列表", description = "获取用户已分配的角色列表")
    @GetMapping("user-roles")
    public Resp<List<Long>> getUserRoles(@Validated @NotNull(message = "未指定用户ID") Long userId) {
        return Resp.success(getBaseService().getUserRoles(userId));
    }

    /**
     * 设置用户角色
     *
     * @param userId
     * @param roleIds
     * @return
     */
    @ApiOperationSupport(order = 100)
    @Operation(summary = "分配角色", description = "为用户分配角色")
    @PostMapping("grant-roles")
    public Resp grantRoles(@RequestParam Long userId, @RequestParam  List<Long> roleIds){
        getBaseService().grantRoles(userId, roleIds);
        return Resp.success();
    }

}
